package com.moon;

import java.util.List;

import cn.yueliangbaba.R;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.loopj.android.http.RequestParams;
import com.sys.DemoApp;
import com.umeng.message.UmengRegistrar;
import com.util.MgqDataHandler;
import com.util.MgqRestClient;
import com.util.PreferencesUtils;
import com.util.ToastUtil;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class ModifyClassActivity extends BaseActivity implements OnClickListener{
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_modify_class);
		
		((TextView) findViewById(R.id.center)).setText("修改班级备注");
		((Button) findViewById(R.id.right)).setText("修改");
		
		JSONArray classes = JSON.parseArray(PreferencesUtils.getString(this, "classes"));
		int size = classes.size();
		for(int i = 0; i < size; i++)
		{
			JSONObject item = classes.getJSONObject(i);
			if(item.getString("id").equals(getIntent().getStringExtra("classId")))
			{
				((EditText) findViewById(R.id.text0)).setText(item.getString("class_alias"));
			}
		}
		
		findViewById(R.id.left).setOnClickListener(this);
		findViewById(R.id.right).setOnClickListener(this);
		findViewById(R.id.layout).setOnClickListener(this);
	}

	@Override
	public void onClick(View arg0) {
		EditText edit=(EditText)findViewById(R.id.text0);  
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
        imm.hideSoftInputFromWindow(edit.getWindowToken(),0);		
		
		int viewId = arg0.getId();
		switch(viewId)
		{
		case R.id.left:
			finish();
			break;
		
		case R.id.right:
			sendRequest();
			break;
		}
	}
	
	private void sendRequest() {
		final String nick = ((EditText)findViewById(R.id.text0)).getText().toString();		
		MgqDataHandler loginHandler = new MgqDataHandler(this, false, false) {
			@Override
			public void onSuccess(String response) {
				JSONObject ruser = JSON.parseObject(response);
				if (ruser.getString("statusCode").equals("0")) {
					ToastUtil.showToastWithAlertPic("修改成功");
					loginBack();
					
					List<Activity> list = ((DemoApp)getApplicationContext()).getActivityList();
					((ClassmateActivity)list.get(list.size() - 2)).setBak(nick);
					
					finish();
				}
				else 
				{
					ToastUtil.showToastWithAlertPic(ruser.getString("message"));
				}
			}
			
			@Override
			public void onFailure(Throwable ble) {
				super.onFailure(ble);
				ToastUtil.showToastWithAlertPic("网络状况不佳，请稍后重试...");
			}
		};
		
		RequestParams params = new RequestParams();
		params.put("id", PreferencesUtils.getString(this, "userId"));
		params.put("class_id", getIntent().getStringExtra("classId"));
		params.put("alias", nick);
		MgqRestClient.get("set_class_alias", params, loginHandler);
	}
	
	private void loginBack() {
		MgqDataHandler loginHandler = new MgqDataHandler(this, false, false) {
			@Override
			public void onSuccess(String response) {
				JSONObject ruser = JSON.parseObject(response);
				if (ruser.getString("statusCode").equals("0")) {
					JSONObject member = ruser.getJSONObject("member");
					
					PreferencesUtils.putString(ModifyClassActivity.this, "member", JSON.toJSONString(member));
					PreferencesUtils.putString(ModifyClassActivity.this, "classes", JSON.toJSONString(member.getJSONArray("classes")));
					PreferencesUtils.putString(ModifyClassActivity.this, "children", JSON.toJSONString(member.getJSONArray("children")));

					((DemoApp)getApplicationContext()).setClasses();
				}
				else 
				{
					ToastUtil.showToastWithAlertPic(ruser.getString("message"));
				}
			}
			
			@Override
			public void onFailure(Throwable ble) {
				super.onFailure(ble);
				ToastUtil.showToastWithAlertPic("网络状况不佳，请稍后重试...");
			}
		};
		
		RequestParams params = new RequestParams();
		params.put("phonenumber", PreferencesUtils.getString(this, "username"));
		params.put("password", PreferencesUtils.getString(this, "password"));
		params.put("android_token", UmengRegistrar.getRegistrationId(this));//ios_token
		MgqRestClient.get("member_login", params, loginHandler);
	}
}
